package cn.blnp.net.flowable.boot.localize.task.notice;

import cn.blnp.net.flowable.boot.service.TaskCarbonCopyService;
import cn.blnp.net.flowable.boot.service.TaskRecallService;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.*;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.flowable.task.api.TaskQuery;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.util.Arrays;

/**
 * <h3>任务知会案例演示</h3>
 *
 * @author <a href="mailto:blnp.yibin@qq.com">lyb</a>
 * @version 1.0
 * @since 2025/6/20 10:28
 */
@Slf4j
@SpringBootTest
public class FlowTaskNoticeTest {

    @Resource
    private RepositoryService repositoryService;
    @Resource
    private RuntimeService runtimeService;
    @Resource
    private IdentityService identityService;
    @Resource
    private TaskService taskService;
    @Resource
    private HistoryService historyService;
    @Resource
    private TaskRecallService taskRecallService;
    @Resource
    private ManagementService managementService;
    @Resource
    private TaskCarbonCopyService taskCarbonCopyService;


    /**
     *  <h2>任务知会案例演示</h2>
     *  <ul>
     *      <li><b>流程定义文件：</b>{@link flow/taskCarbonCopyProcess.bpmn20.xml}</li>
     *      <li>
     *          <b>流程预览图：</b></br>
     *          <img src="https://cdn4.winhlb.com/2025/06/20/6854c841ef7d8.png" >
     *      </li>
     *  </ul>
     **/
    @Test
    public void test() {
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("flow/taskCarbonCopyProcess.bpmn20.xml")
                .name("任务知会案例")
                .deploy();
        log.info("部署ID：{}", deploy.getId());
        //流程定义信息
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .deploymentId(deploy.getId())
                .singleResult();
        log.info("流程定义ID：{}", processDefinition.getId());
        //启动流程实例
        ProcessInstance procInst = runtimeService.startProcessInstanceById(processDefinition.getId());
        log.info("流程实例ID：{}", procInst.getId());

        TaskQuery taskQuery = taskService.createTaskQuery()
                .processInstanceId(procInst.getId());
        //查询并办理第一个任务
        Task firstTask = taskQuery.singleResult();
        taskService.complete(firstTask.getId());
        //查询第二个任务
        Task secondTask = taskQuery.singleResult();

        //分别为两个任务添加知会人
        taskCarbonCopyService.executeTaskCarbonCopy(firstTask.getId(), Arrays.asList("安琪拉", "妲己"));
        taskCarbonCopyService.executeTaskCarbonCopy(secondTask.getId(), Arrays.asList("大乔"));
        //查询 妲己 的已办任务数
        long hisTaskNum = historyService.createHistoricTaskInstanceQuery()
                .taskAssignee("妲己").processInstanceId(procInst.getId())
                .count();
        //查询 妲己 的历史知会数
        long hisCarbonCopyNum = taskCarbonCopyService
                .getHistoricCarbonCopyTasks("妲己")
                .stream()
                .filter(hisTask -> hisTask.getProcessInstanceId().equals(procInst.getId()))
                .count();
        log.info("{}在流程实例{}下的已办任务数为{}，历史知会数为{}", "妲己",
                procInst.getId(), hisTaskNum, hisCarbonCopyNum);
        //查询 大乔 的待办任务数
        long taskNum = taskQuery.taskCandidateOrAssigned("大乔").count();
        //查询 大乔 的知会数
        long carbonCopyNum = taskCarbonCopyService.getCarbonCopyTasks("大乔")
                .stream()
                .filter(task -> task.getProcessInstanceId().equals(procInst.getId()))
                .count();
        log.info("{}在流程实例{}下的待办任务数为{}，知会数为{}", "大乔", procInst.getId(), taskNum, carbonCopyNum);

        /**
         *  演示结果：
         *  2025-06-20 10:39:25.205  INFO 18548 --- [           main] c.b.n.f.b.l.t.notice.FlowTaskNoticeTest  : 部署ID：265001
         * 2025-06-20 10:39:25.214  INFO 18548 --- [           main] c.b.n.f.b.l.t.notice.FlowTaskNoticeTest  : 流程定义ID：taskCarbonCopyProcess:1:265004
         * 2025-06-20 10:39:25.288  INFO 18548 --- [           main] c.b.n.f.b.l.t.notice.FlowTaskNoticeTest  : 流程实例ID：265005
         * 2025-06-20 10:39:25.406  INFO 18548 --- [           main] c.b.n.f.b.l.t.notice.FlowTaskNoticeTest  : 妲己在流程实例265005下的已办任务数为0，历史知会数为1
         * 2025-06-20 10:39:25.471  INFO 18548 --- [           main] c.b.n.f.b.l.t.notice.FlowTaskNoticeTest  : 大乔在流程实例265005下的待办任务数为0，知会数为1
         **/
    }
}
